#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QThread>
#include <QtDebug>
#include "Image.h"
#include "QPixmap"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 默认显示窗口
    ui->stackedWidget->setCurrentWidget(ui->win1);
    ui->stackedWidget->setCurrentWidget(0);



    //切换主窗口
    connect(ui->show_win1, &QPushButton::clicked, this, [=]()
        {
        // 切换显示第一个子窗口
            ui->stackedWidget->setCurrentIndex(1);
        });

    connect(ui->show_win2, &QPushButton::clicked, this, [=]()
        {
        // 切换显示第二个子窗口
            ui->stackedWidget->setCurrentIndex(0);
        });


    //切换小窗口
    connect(ui->pushButton_2, &QPushButton::clicked, this, [=]()
        {
        // 缩放窗口
            ui->stackedWidget_2->setCurrentIndex(0);
        });

    connect(ui->pushButton_3, &QPushButton::clicked, this, [=]()
        {
        // 剪切窗口
            ui->stackedWidget_2->setCurrentIndex(1);
        });

    connect(ui->pushButton_4, &QPushButton::clicked, this, [=]()
        {
        // 旋转窗口
            ui->stackedWidget_2->setCurrentIndex(2);
        });

    connect(ui->pushButton_21, &QPushButton::clicked, this, [=]()
        {
        // 亮度窗口
            ui->stackedWidget_2->setCurrentIndex(4);
        });

    connect(ui->pushButton_23, &QPushButton::clicked, this, [=]()
        {
        // 翻转窗口
            ui->stackedWidget_2->setCurrentIndex(5);
        });

    connect(ui->pushButton_7, &QPushButton::clicked, this, [=]()
        {
            ui->stackedWidget_2->setCurrentIndex(3);
        });

    connect(ui->pushButton_9, &QPushButton::clicked, this, [=]()
        {
            ui->stackedWidget_2->setCurrentIndex(3);
        });

    connect(ui->pushButton_10, &QPushButton::clicked, this, [=]()
        {
            ui->stackedWidget_2->setCurrentIndex(3);
        });

    connect(ui->pushButton_11, &QPushButton::clicked, this, [=]()
        {
            ui->stackedWidget_2->setCurrentIndex(3);
        });

    connect(ui->pushButton_12, &QPushButton::clicked, this, [=]()
        {
            ui->stackedWidget_2->setCurrentIndex(3);
        });

    connect(ui->pushButton_13, &QPushButton::clicked, this, [=]()
        {
            ui->stackedWidget_2->setCurrentIndex(3);
        });

    connect(ui->pushButton_14, &QPushButton::clicked, this, [=]()
        {
            ui->stackedWidget_2->setCurrentIndex(3);
        });

    connect(ui->pushButton_15, &QPushButton::clicked, this, [=]()
        {
            ui->stackedWidget_2->setCurrentIndex(3);
        });

    connect(ui->pushButton_16, &QPushButton::clicked, this, [=]()
        {
            ui->stackedWidget_2->setCurrentIndex(3);
        });

    connect(ui->pushButton_19, &QPushButton::clicked, this, [=]()
        {
            ui->stackedWidget_2->setCurrentIndex(3);
        });

}

Image *img0 = new Image; //输入
Image *img1 =new Image; //输出

// 导入图片
void MainWindow::on_lead_img_triggered()
{
    QString curDir=QDir::currentPath();//选择文件
    QString filename=QFileDialog::getOpenFileName(this,"select Image",curDir, "Images (*.bmp )");

    if(filename.isEmpty())
    {
         return;
    }
    std::string str = filename.toStdString();
    const char* ch = str.c_str();
    img0->ReadBMP(ch);

    img0->Flip(1);
    //img0->Resize(ui->showlabel_1->height(),ui->showlabel_1->width());  //调整大小

    ui->showlabel_1->setPixmap(QPixmap::fromImage(*img0));
    *img1 = Image( *img0 );
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));

}

// 保存图片
void MainWindow::on_save_img_triggered()
{
    img1->Flip(1);
    QString curDir=QDir::currentPath();//选择文件
    QString filename=QFileDialog::getSaveFileName(this,"save Image",curDir, "Images (*.bmp)");

    if(filename.isEmpty())
    {
         return;
    }
    std::string str = filename.toStdString();
    const char* ch = str.c_str();
    img1->WriteBMP(ch);
    img1->Flip(1);
}

//剪切
void MainWindow::on_pushButton_5_clicked()
{
    img1->Cut(ui->lineEdit_2->text().toInt(), ui->lineEdit_3->text().toInt(), ui->lineEdit_4->text().toInt(), ui->lineEdit_5->text().toInt() );
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

//灰度化
void MainWindow::on_pushButton_10_clicked()
{
    img1->gray();
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

//均值滤波
void MainWindow::on_pushButton_9_clicked()
{
    img1->mean_filter();
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

//高斯滤波
void MainWindow::on_pushButton_7_clicked()
{
    img1->Gaussian_filter(/*int flage =*/ 0);
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

//中值滤波
void MainWindow::on_pushButton_11_clicked()
{
   img1->mid_value_filter();
   ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

//阈值处理
void MainWindow::on_pushButton_12_clicked()
{
    img1->threshold(/*int thresh =*/ 127, /*int maxval =*/ 255, /*int type =*/ 0);
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

//Sobel梯度运算
void MainWindow::on_pushButton_13_clicked()
{
    img1->Sobel();
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

//伽马变换
void MainWindow::on_pushButton_14_clicked()
{
    img1->gamma();
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

// 边缘检测
void MainWindow::on_pushButton_15_clicked()
{
    img1->Canny(/*int minVale =*/ 70, /*int maxVale =*/ 110);
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

// 马赛克
void MainWindow::on_pushButton_16_clicked()
{
    img1->Mosaic();
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

// 高斯金字塔->放大
void MainWindow::on_pushButton_17_clicked()
{
    img1->upper();
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

// 高斯金字塔->缩小
void MainWindow::on_pushButton_18_clicked()
{
    img1->lower();
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

// 缩放
void MainWindow::on_pushButton_clicked()
{
    img1->Resize(ui->lineEdit_7->text().toInt(),ui->lineEdit->text().toInt());
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

// 去雾
void MainWindow::on_pushButton_19_clicked()
{
    img1->remHaze(/*int minwinSize  =*/ 3,/*int tranwinSize =*/ 3, /*double tran =*/ 0.1, /*double w =*/ 0.95);
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

//图像旋转的函数（旋转角度为90度的整数倍）
//返回值：1 输入值不合要求（不是90的倍数）
void MainWindow::on_pushButton_6_clicked()
{
    img1->Rotate(ui->lineEdit_6->text().toInt());
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

//顺时针90°旋转
void MainWindow::on_pushButton_8_clicked()
{
    img1->Rotate(90);
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

//逆时针90°旋转
void MainWindow::on_pushButton_20_clicked()
{
    img1->Rotate(270);
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

// 调亮度
void MainWindow::on_pushButton_22_clicked()
{
    img1->brightAdj(ui->lineEdit_8->text().toInt());
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

//上下翻转
void MainWindow::on_pushButton_26_clicked()
{
    img1->Flip(1);
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

//左右翻转
void MainWindow::on_pushButton_25_clicked()
{
    img1->Flip(0);
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

//还原
void MainWindow::on_pushButton_24_clicked()
{
    *img1 = Image( *img0 );
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

//还原
void MainWindow::on_pushButton_27_clicked()
{
    *img1 = Image( *img0 );
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}

//还原
void MainWindow::on_pushButton_28_clicked()
{
    *img1 = Image( *img0 );
    ui->showlabel_2->setPixmap(QPixmap::fromImage(*img1));
}



MainWindow::~MainWindow()
{
    delete ui;
}
